//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    imgUrls: [
     
    ],
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    coupondisplay: 'none',
    specials2: [
      { id: '1', title: "拼团", description: "拼团价更惠", tip: "拼团" ,img:""},
      { id: '2', title: '更多', description: "更多等你来", tip: "热卖", img:""}
    ],
    coupon:{},
    specials:[],
    recommends:[]
  },
  onLoad: function () {
    var that = this;

    // 轮播
    wx.request({
      url: app.globalData.directory + 'api/v2/banner/1', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            imgUrls:res.data.data.item
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });

    //特惠商品
    wx.request({
      url: app.globalData.directory + 'api/v2/banner/2', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync("token")
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          var specials2 = that.data.specials2
          for(var i=0;i<specials2.length;i++){
            specials2[i].img = res.data.data.item[i].img
          }
          that.setData({
            specials2: specials2
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });

    //中间小导航优惠券
    wx.request({
      url: app.globalData.directory + 'api/v2/getNewActivity',
      data: {},
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
		// console.log(res);
        if (res.data.status == 'ok') {
          if (!res.data.data.is_have_coupon == 0) {
            that.setData({
              coupon: res.data.data,
              coupondisplay: 'block'
            })
          } else {
            that.setData({
              coupondisplay: 'none'
            })
          }

        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })

    // 热销
    wx.request({
      url: app.globalData.directory + 'api/v2/getHomePageRecommend', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        // console.log(res)
        if (res.data.status == 'ok') {
          that.setData({
            recommends:res.data.data
          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });
  },
  huodong:function(){
    wx.showToast({
      title: '此活动暂未开启',
      icon: 'none',
      duration: 2000,
    })
  },
  pinOrAll:function(e){
     var id = e.currentTarget.dataset.id
     if(id == "1"){
       wx.navigateTo({
         url: '../collage/collage',
       })
     }else{
       wx.switchTab({
         url: '../order/order',
       })
     }
  }
 
})  
